#include "mem.h"
#include "romfile.h"
#include<cstdio>
using namespace std;
mem::mem(romfile * rf) {
    rom=rf->contents;
    //oam, ram, vram
    hram=new unsigned char[127];
    if(rf->sram) {
        sram=new unsigned char[(rf->ram_banks)*2048];
    }
    oam=new unsigned char[160];
    ram=new unsigned char[32768];
    vram=new unsigned char[16384];
    mapper=rf->mapper;
    rom_offset=0;
    ram_offset=0;
    vram_offset=0;
    mask_fw=false;

}

void mem::write_byte(unsigned short addr, unsigned char val) {
    printf("Writing byte %02x into address %04x (not implemented!)\n",val,addr);
}

void mem::write_word(unsigned short addr, unsigned short val) {
    printf("Writing word %04x into address %04x (not implemented!)\n",val,addr);
}

unsigned char mem::read_byte(unsigned short addr) {
    printf("Reading %04x (",addr);
    if(addr<0x4000) {
        printf("%02x)\n",rom[addr]);
        return rom[addr];

    }
    else if(addr<0x8000) {
        printf("%02x)\n",rom[addr+rom_offset]);
        return rom[addr+rom_offset];
    }
    else {
        printf("not implemented)\n");
    }
    return 0; //default return;
}

unsigned short mem::read_word(unsigned short addr) {
    if(addr<0x4000) {
        return (rom[addr])+((rom[addr+1])<<(8));

    }
    else if(addr<0x8000) {
        return (rom[addr+rom_offset])+((rom[addr+1+rom_offset])<<(8));
    }
    else {
        printf("Reading word from address %04x (not implemented!)\n",addr);
    }
    return 0; //default return
}
